package com.cloud.system.controller;

import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.entity.SysPost;
import com.cloud.enums.BusinessType;
import com.cloud.common.enums.CheckType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.user_info.service.ISysPostService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author hhb, yzj
 */

@Tag(name = "岗位信息")
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {

    @Autowired
    private ISysPostService postService;

    @Operation(summary = "查询岗位列表")
    @RequestPermissions("system:post:list")
    @GetMapping("/list")
    public Result list(SysPost post) {
        startPage();
        List<SysPost> list = postService.selectPostList(post);
        return getDataTable(list);
    }

    @Operation(summary = "导出岗位列表")
    @Logs(title = "岗位管理", businessType = BusinessType.EXPORT)
    @RequestPermissions("system:post:export")
    @GetMapping("/export")
    public void export(SysPost post) {
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysPost> list = postService.selectPostList(post);
        String titleName = "系统岗位列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_post", SysPost.class,
                titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "查询岗位详细")
    @Parameter(name = "postId", description = "岗位ID")
    @RequestPermissions("system:post:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long postId) {
        return Result.success("查询成功", postService.selectPostById(postId));
    }

    @Operation(summary = "新增岗位信息")
    @Logs(title = "新增岗位信息", businessType = BusinessType.INSERT)
    @RequestPermissions("system:post:add")
    @PostMapping("/insert")
    public Result insert(@RequestBody SysPost post) {
        if (CheckType.NOT_UNIQUE.getCode().equals(postService.checkPostNameUnique(post))) {
            return Result.error("新增岗位[" + post.getPostName() + "]失败,岗位名称已存在");
        } else if (CheckType.NOT_UNIQUE.getCode().equals(postService.checkPostCodeUnique(post))) {
            return Result.error("新增岗位[" + post.getPostName() + "]失败,岗位编码已存在");
        }
        return toResult(postService.insertPost(post));
    }

    @Operation(summary = "修改岗位信息")
    @Logs(title = "修改岗位信息", businessType = BusinessType.UPDATE)
    @RequestPermissions("system:post:edit")
    @PostMapping("/update")
    public Result update(@RequestBody SysPost post) {
        if (CheckType.NOT_UNIQUE.getCode().equals(postService.checkPostNameUnique(post))) {
            return Result.error("修改岗位[" + post.getPostName() + "]失败,岗位名称已存在");
        } else if (CheckType.NOT_UNIQUE.getCode().equals(postService.checkPostCodeUnique(post))) {
            return Result.error("修改岗位[" + post.getPostName() + "]失败,岗位编码已存在");
        }
        return toResult(postService.updatePost(post));
    }

    @Operation(summary = "删除岗位信息")
    @Parameter(name = "postIds", description = "岗位ID[多个用,隔开]")
    @Logs(title = "删除岗位信息", businessType = BusinessType.DELETE)
    @RequestPermissions("system:post:remove")
    @PostMapping("/remove")
    public Result remove(String postIds) {
        List<Long> list = ToolUtils.setIdsToList(postIds);
        return toResult(postService.deletePostByIds(list));
    }

    @Operation(summary = "获取岗位选择框列表")
    @GetMapping("/optionSelect")
    public Result optionSelect() {
        return Result.success("查询成功", postService.selectAllPostList());
    }

}
